package com.training.food.controller;

import java.util.List;

import com.training.food.Domain.ExamineRecord;
import com.training.food.Domain.RecordResult;
import com.training.food.pojo.Examine;
import com.training.food.pojo.Recordinfo;
import com.training.food.pojo.User;
import com.training.food.service.ExamineService;
import com.training.food.service.LoginService;
import com.training.food.service.RecordService;
import com.training.food.service.RegisterService;
import com.training.food.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
@RestController
public class RecordController {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    RecordService recordService;
    @Resource
    ExamineService examineService;
    //备案
    @PostMapping("/food/user/hasRecord")
    public CommonResult hasRecode(HttpServletRequest request, @RequestBody Recordinfo recordinfo) throws UnsupportedEncodingException {
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        recordinfo.setUsername(user.getUsername());
        System.out.println(recordinfo);
        if(!recordService.hasRecord(recordinfo)){
            recordService.record(recordinfo);
            Examine examine=new Examine();
            examine.setUsername(user.getUsername());
            examine.setState('0');
            if(examineService.getById(examine.getUsername())==null) {
                examineService.save(examine);
            }
            return CommonResult.success("备案成功");
        }
        return CommonResult.failed("备案已存在");

    }
    //返回审核中的备案信息
    @PostMapping("/food/user/examineRecord")
    public CommonResult examineRecord(HttpServletRequest request){
        ExamineRecord examineRecord=new ExamineRecord();
        List<Examine>examines=examineService.lists();
        examineRecord.setExamine(examines);
        examineRecord.setRecordinfo(recordService.records(examines));
        return CommonResult.success(examineRecord);
    }
    //查询备案信息
    @PostMapping("/food/user/selectRecode")
    public CommonResult selectRecord(HttpServletRequest request)throws UnsupportedEncodingException{
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        RecordResult recordResult=new RecordResult();
        if(user!=null) {
            Recordinfo recordinfo = recordService.select(user.getUsername());
            Examine examine = new Examine();
            examine.setUsername(user.getUsername());
            examine = examineService.select(examine);
            recordResult.setRecordinfo(recordinfo);
            recordResult.setExamine(examine);
            return CommonResult.success(recordResult);
        }
        return CommonResult.failed("token不存在");
    }
    @PostMapping("/food/user/updateRecode")
    //修改备案信息
    public CommonResult updateRecode(HttpServletRequest request, @RequestBody Recordinfo recordinfo) throws UnsupportedEncodingException{
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;

        recordinfo.setUsername(user.getUsername());
        if(recordService.hasRecord(recordinfo)){
            recordService.updateRecode(recordinfo);
            Examine examine=new Examine();
            examine.setUsername(user.getUsername());
            examine=examineService.select(examine);
            examine.setState('1');
            examineService.update(examine);
            return CommonResult.success("修改备案信息成功");
        }
        return CommonResult.failed("备案不存在");
    }

    //增加审核
    @PostMapping("/food/user/examine")
    public CommonResult examine(HttpServletRequest request, @RequestBody Examine examine){
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        examine.setUsername(user.getUsername());
        if(examineService.getById(examine.getUsername())==null) {
            examineService.save(examine);
            return CommonResult.success("设置审核信息成功");
        }
        return CommonResult.failed("设置审核信息失败");
    }
    //修改审核信息
    @PostMapping("/food/user/updateExamine")
    public CommonResult updateExamine(HttpServletRequest request, @RequestBody Examine examine){
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        examine.setUsername(examine.getUsername());
        //System.out.println(examine);
        if(examineService.select(examine)!=null) {
            examineService.update(examine);
            return CommonResult.success("设置审核信息成功");
        }
        return CommonResult.failed("审核信息不存在");
    }
    //查看审核信息
    @PostMapping("/food/user/showExamine")
    public CommonResult showExamine(HttpServletRequest request){
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        if(user!=null) {
            Examine examine = new Examine();
            examine.setUsername(user.getUsername());
            examine = examineService.select(examine);
            if (examine != null) {
                return CommonResult.success(examine);
            }
        }
        return CommonResult.failed("审核信息不存在");
    }
    //返回所有备案信息
    @PostMapping("/food/user/selectRecords")
    public CommonResult selectRecords(){
        return CommonResult.success(recordService.selectRecords());
    }
    //返回所有用户信息
    @PostMapping("/food/user/selectUsers")
    public CommonResult selectUsers(){
        return CommonResult.success(recordService.selectUsers());
    }
    //根据用户名返回用户信息
    @PostMapping("/food/user/selectByUsername")
    public CommonResult selectByUsername(@RequestBody User user){
        return CommonResult.success(recordService.select(user.getUsername()));
    }
    //根据状态返回所有用户信息
    @PostMapping("/food/user/selectByState")
    public CommonResult selectByState(){
        List<Examine>examines=examineService.lists();
        return CommonResult.success(recordService.records(examines));
    }
    //根据token获取是否拥有备案信息
    @PostMapping("/food/user/getIfRecord")
    public CommonResult getIfRecord(HttpServletRequest request){
        String token= (String) request.getAttribute("user");
        Object user=redisTemplate.opsForValue().get(token);
        User user1=(User)user;
        //System.out.println(user1);
        if(user!=null){
            Recordinfo recordinfo=recordService.select(user1.getUsername());
            if(recordinfo!=null){
                Examine examine=new Examine();
                examine.setUsername(recordinfo.getUsername());
                examine=examineService.select(examine);
                if(examine!=null){
                    if(examine.getState()!='2'){
                        return CommonResult.success("拥有未被冻结的备案信息");
                    }
                    return CommonResult.failed("备案信息被冻结");
                }
                return CommonResult.failed("未查询到备案信息");
            }
        }
        return CommonResult.failed("未查询到用户");
    }

}
